package edu.hitwh.service;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import edu.hitwh.record.BorrowerRecord;
import edu.hitwh.record.LibrarianRecord;
import edu.hitwh.respository.BorrowerRepository;
import edu.hitwh.respository.LibrarianRepository;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class AuthService {
    private final PasswordEncoder passwordEncoder;
    private final BorrowerRepository borrowerRepository;
    private final LibrarianRepository librarianRepository;

    public boolean authenticateBorrower(BorrowerRecord borrowerRecord) {
        var borrower = borrowerRepository.findByEmail(borrowerRecord.getEmail());
        if (borrower.isEmpty()) {
            return false;
        }
        return passwordEncoder.matches(borrowerRecord.getPassword(), borrower.get().getPassword());
    }

    public boolean authenticateLibrarian(LibrarianRecord librarianRecord) {
        var librarian = librarianRepository.findByEmail(librarianRecord.getEmail());
        if (librarian.isEmpty()) {
            return false;
        }
        return passwordEncoder.matches(librarianRecord.getPassword(), librarian.get().getPassword());
    }
}
